﻿using System;
using System.Runtime.InteropServices;
using System.Security;

namespace Neoseeker.NAMFox.Tests.Infrastructure
{
    /// <summary>
    /// Contains methods that are safe to invoke without security checks.
    /// </summary>
    [SuppressUnmanagedCodeSecurity]
    internal class SafeNativeMethods
    {
        /// <summary>
        /// Retrieves information about the current system to an application running under WOW64.
        /// If the function is called from a 64-bit application, it is equivalent to the GetSystemInfo function.
        /// </summary>
        /// <param name="lpSystemInfo">A pointer to a SYSTEM_INFO structure that receives the information.</param>
        [DllImport("kernel32.dll")]
        public static extern void GetNativeSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);

        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEM_INFO
        {
            internal _PROCESSOR_INFO_UNION uProcessorInfo;
            public uint dwPageSize;
            public IntPtr lpMinimumApplicationAddress;
            public IntPtr lpMaximumApplicationAddress;
            public IntPtr dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public ushort dwProcessorLevel;
            public ushort dwProcessorRevision;
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct _PROCESSOR_INFO_UNION
        {
            [FieldOffset(0)]
            internal uint dwOemId;
            [FieldOffset(0)]
            internal ProcessorArchitecture wProcessorArchitecture;
            [FieldOffset(2)]
            internal ushort wReserved;
        }

        public enum ProcessorArchitecture : ushort
        {
            x86 = 0,
            IA64 = 6,
            AMD64 = 9,
            Unknown = 0xFFFF
        }
    }
}
